//
//  Test.m
//  OCDemo
//
//  Created by wxf on 2024/8/11.
//

#import "Test.h"
#import "Persion.h"
#import "Student.h"
#import "Teacher.h"
#import "SingleTest.h"
#import "TestNSDate.h"
#import "TestFile.h"


@implementation Test


- (void)testFile {
    TestFile *testFile = [TestFile new];
    [testFile testBase];
}


//测试description - 类似java toString 方法
- (void)test1{
    NSLog(@"测试description - 类似java toString 方法");
    //新建对象
    Persion *per = [Persion new];
    NSLog(@"per = %@", per);
    per.age = 18;
    per.sex = 1;
    per.weight = 180;
    per.height = 180;
    per.name = @"zy";
    NSLog(@"per = %@", per);
}

//测试多态
- (void)test2{
    NSLog(@"测试多态 - 父类指向子类对象");
    Persion *tea = [Teacher new];
    [tea run];
}


- (void)  test3{
    NSLog(@"测试构造方法");
    //开辟空间  alloc
    //初始化    init
    //返回地址  init 
    Persion *per = [Persion new];
    per.age = 18;
    per.name = @"zy";
    
    NSLog(@"per = %@", per);
    
    Persion *per1 = [[Persion alloc]init];
    NSLog(@"per1 = %@", per1);
    Persion *per2 = [[Persion alloc]initWithAge:18];
    NSLog(@"per2 = %@", per2);
    
    Persion *per3 = [[Persion alloc]initWithName:@"zy0811"];
    NSLog(@"per3 = %@", per3); 
    
    
    Persion *per4 = [[Persion alloc]initWithNameAndAge:@"zy0811" andAge:18];
    NSLog(@"per4 = %@", per4);
}

- (void)test4{
    NSLog(@"测试不可变字符串");
    //简单方式创建
    NSString *str1 = @"Hello OC";
    NSLog(@"str1 = %@", str1);
    //通过已存在的字符串创建
    NSString *str2 = [[NSString alloc] initWithString:str1];
    NSLog(@"str2 = %@", str2);
    //通过C语言字符串
    char *str  = "Hello World!";
    NSString *str3 = [[NSString alloc] initWithUTF8String:str];
    NSLog(@"str3 = %@", str3);
    
    //格式化字符串
    NSString *str4 = [[NSString alloc] initWithFormat: @"%@-%@ ",str1,str2];
    NSLog(@"str4 = %@", str4);
    
    //通过类方法
    //通过已存在的字符串创建
    NSString *str5 = [NSString stringWithString:str4];
    NSLog(@"str5 = %@", str5);
    //通过C语言字符串
    NSString *str6 = [NSString stringWithUTF8String:str];
    NSLog(@"str6 = %@", str6);
    
    //格式化字符串
    NSString *str7 = [NSString stringWithFormat:@"%@+%@",str5,str6];
    NSLog(@"str7 = %@", str7);
    //拼接方式创建字符串
    NSString *str8 = [str6 stringByAppendingString:str7];
    NSLog(@"str8 = %@", str8);
    
    //格式化拼接方式创建字符串
    NSString *str9 = [str6 stringByAppendingFormat:str7];
    NSLog(@"str9 = %@", str9);
}

- (void)test5{
    NSString *str = @"Hello OC And iOS";
    NSString *str1 = @"Hello OC And iOS";
    NSString *str2 = @"Hello OC And iOS Not";
    NSLog(@"str = %@", str);
    //获取长度
    NSUInteger len = [str length];
    NSLog(@"len = %lu", len);
    
    //获取字符
    char a = [str characterAtIndex:0];
    NSLog(@"0 index = %c", a);
    NSLog(@"1 index = %c", [str characterAtIndex:1]);
    
    //3 比较两个字符串是否相等
    BOOL is1 = [str isEqualToString:str1];
    BOOL is2 = [str isEqualToString:str2];
    if (is1) {
        NSLog(@"str isEqualToString:str1");
    }else{
        NSLog(@"str not isEqualToString:str1");
    }
    
    if (is2) {
        NSLog(@"str isEqualToString:str2");
    }else{
        NSLog(@"str not isEqualToString:str2");
    }
    
    //比较两个字符串的大小，区分大小写
    NSString *str3 = @"bb";
    NSString *str4 = @"bb";
    NSString *str5 = @"BB";
    NSString *str6 = @"aa";
    NSString *str7 = @"cc";
    NSLog(@"字符串比较大小区分大小写");
    [self testNSStringCompare:str3 withS2:str4];
    [self testNSStringCompare:str3 withS2:str5];
    [self testNSStringCompare:str3 withS2:str6];
    [self testNSStringCompare:str3 withS2:str7];
    
    
    NSLog(@"字符串比较大小 不区分大小写");
    [self testNSStringCompare:str3 withS2:str4 options:NSCaseInsensitiveSearch];
    [self testNSStringCompare:str3 withS2:str5 options:NSCaseInsensitiveSearch];
    [self testNSStringCompare:str3 withS2:str6 options:NSCaseInsensitiveSearch];
    [self testNSStringCompare:str3 withS2:str7 options:NSCaseInsensitiveSearch];
    
    
    NSLog(@"字符串 大小写转换");

    NSLog(@"小写转大写:%@", [@"gege ai meimei" uppercaseString]);
    NSLog(@"大写转小写:%@", [@"HAHA" lowercaseString]);
    NSLog(@"首字母大写:%@", [@"hello world" capitalizedString]);
    
    NSLog(@"字符串 查找字符串");
    NSRange range = [@"hello dage he xiaomei" rangeOfString:@"dage"];
    if(range.location == NSNotFound){
        NSLog(@"没有找到");
    }else if(range.location != NSNotFound){
        NSLog(@"找到了");
    }
    
    if(range.length == 0){
        NSLog(@"没有找到");
    }else if(range.length != 0){
        NSLog(@"找到了");
    }
    
    NSLog(@"range location=%lu length = %lu",range.location,range.length);
    NSRange range1 = [@"hello dage he xiaomei" rangeOfString:@"dog"];
    NSLog(@"range1 location=%lu length = %lu",range1.location,range1.length);
    
    
    NSLog(@"字符串 转成数字 = %d", [@"123" intValue]);
    NSLog(@"字符串 转成数字 = %d", [@"+123" intValue]);
    NSLog(@"字符串 转成数字 = %d", [@"-123" intValue]);
    NSLog(@"字符串 转成数字 = %d", [@"12a3" intValue]);
    
    
    NSString *str8 =@"abcedfg";
    NSLog(@"字符串 字符串提取substringToIndex = %@", [str8 substringToIndex:0]);
    NSLog(@"字符串 字符串提取substringToIndex = %@", [str8 substringToIndex:1]);
    NSLog(@"字符串 字符串提取substringToIndex = %@", [str8 substringToIndex:2]);
    NSLog(@"字符串 字符串提取substringToIndex = %@", [str8 substringToIndex:3]);
    NSLog(@"字符串 字符串提取substringToIndex = %@", [str8 substringToIndex:4]);
    
    NSLog(@"字符串 字符串提取substringFromIndex = %@", [str8 substringFromIndex:0]);
    NSLog(@"字符串 字符串提取substringFromIndex = %@", [str8 substringFromIndex:1]);
    NSLog(@"字符串 字符串提取substringFromIndex = %@", [str8 substringFromIndex:2]);
    NSLog(@"字符串 字符串提取substringFromIndex = %@", [str8 substringFromIndex:3]);
    NSLog(@"字符串 字符串提取substringFromIndex = %@", [str8 substringFromIndex:4]);
    
    NSLog(@"字符串 字符串提取substringWithRange = %@", [str8 substringWithRange:NSMakeRange(0, 0)]);
    NSLog(@"字符串 字符串提取substringWithRange = %@", [str8 substringWithRange:NSMakeRange(0, 1)]);
    NSLog(@"字符串 字符串提取substringWithRange = %@", [str8 substringWithRange:NSMakeRange(0, 2)]);
    NSLog(@"字符串 字符串提取substringWithRange = %@", [str8 substringWithRange:NSMakeRange(1, 2 )]);
}

- (void)testNSStringCompare:(NSString *)s1 withS2:(NSString *)s2 {
    
    NSComparisonResult compareResult = [s1 compare:s2];
    
    if(compareResult == NSOrderedSame){
        NSLog(@"%@ == %@",s1, s2);
    }else if(compareResult == NSOrderedAscending){
        NSLog(@"%@ < %@",s1, s2);
    }else if(compareResult == NSOrderedDescending){
        NSLog(@"%@ > %@",s1, s2);
    }
}

- (void)testNSStringCompare:(NSString *)s1 withS2:(NSString *)s2 options:(NSStringCompareOptions)mask {
    NSComparisonResult compareResult = [s1 compare:s2 options:mask];
    
    if(compareResult == NSOrderedSame){
        NSLog(@"%@ == %@",s1, s2);
    }else if(compareResult == NSOrderedAscending){
        NSLog(@"%@ < %@",s1, s2);
    }else if(compareResult == NSOrderedDescending){
        NSLog(@"%@ > %@",s1, s2);
    }
}

- (void)test6{
    NSLog(@"可变字符串");
    NSLog(@"创建空 可变字符串");
    NSMutableString *str = [[NSMutableString alloc] init];
    NSLog(@"str = %@", str);
    
    NSMutableString *str1 = [NSMutableString stringWithCapacity:20];
    NSLog(@"[str1 = %@]", str1 );
    
    NSMutableString *str2 = [NSMutableString stringWithString:@"hello"];
    NSLog(@"增");
    NSLog(@"str2  = %@", str2);
    NSLog(@"str2  = %@", [str2 stringByAppendingString:@"world"]);
    NSLog(@"str2  = %@", str2);
    
    [str2 appendString:@" world"];
    NSLog(@"str2  = %@", str2);
    
    [str2 insertString:@" gege " atIndex:7];
    NSLog(@"str2  = %@", str2);

    
    NSLog(@"删");
    [str2  deleteCharactersInRange:NSMakeRange(0, 1)];
    NSLog(@"str2  = %@", str2);
    [str2  deleteCharactersInRange:NSMakeRange(0, 1)];
    NSLog(@"str2  = %@", str2);

    
    NSLog(@"改");
    [str2  setString:@"Hello OC"];
    NSLog(@"str2  = %@", str2);
    
    [str2  replaceCharactersInRange:NSMakeRange(6, 2) withString:@"MyiOS"];
    NSLog(@"str2  = %@", str2);
    
    
    NSLog(@"查");
    
    for(int loop = 0; loop < [str2 length]; loop++){
        char ch = [str2 characterAtIndex:loop];
        NSLog(@"%d  = %c", loop, ch);
    }
}

- (void)test7{
    NSLog(@"不可变数组");
    NSLog(@"1.创建空数组");
    NSArray *array1 = [[NSArray alloc]init];
    NSArray *array2 = [NSArray array];
    
    NSLog(@"array1=%@",array1);
    NSLog(@"array2=%@",array2);
    NSLog(@"2.简单方式创建，结尾还是有nil，但是被隐藏了");
    NSArray *array3 = @[@"aa",@"bb",@"cc",@"dd",@"ee"];
    NSLog(@"array3=%@",array3);
    
    NSLog(@"3.对象方法创建");
    NSLog(@"通过已存在的数组创建");
    NSArray *array4 = [[NSArray alloc] initWithArray:array3];
    NSLog(@"array4=%@",array4);
    NSLog(@"简单创建");
    NSArray *array5 = [[NSArray alloc] initWithObjects:@"red",@"black",@"white",@"green",@"blue", nil];
    NSLog(@"array5=%@",array5);
    
    NSLog(@"4.类方法创建");
    NSLog(@"通过已存在的数据创建");
    NSArray *array6 = [NSArray arrayWithArray:array5];
    NSLog(@"array6=%@",array6);
    
    NSArray *array7 = [NSArray arrayWithObjects:@"red",@"black",@"white",@"green",@"blue", nil];
    NSLog(@"array7=%@",array7);
}

- (void)test8{
    NSLog(@"不可变数组常用方法");
    NSArray *array1 = @[@"red",@"black",@"white",@"green",@"blue"];
    NSLog(@"array1=%@",array1);
    
    NSUInteger count =[array1 count];
    NSLog(@"数据长度 count=%lu",count);
    NSLog(@"取元素 [0]=%@",[array1 objectAtIndex:0]);
    NSLog(@"取元素 [1]=%@",[array1 objectAtIndex:1]);
    NSLog(@"取元素 [2]=%@",[array1 objectAtIndex:2]);
    NSLog(@"取元素 [3]=%@",[array1 objectAtIndex:3]);
    
    NSLog(@"简单方式取元素");
    NSLog(@"取元素 [0]=%@",array1[0]);
    NSLog(@"取元素 [1]=%@",array1[1]);
    NSLog(@"取元素 [2]=%@",array1[2]);
    NSLog(@"取元素 [3]=%@",array1[3]);
    
    NSLog(@"取元素 第一个元素=%@",[array1 firstObject]);
    NSLog(@"取元素 最后一个元素=%@",[array1 lastObject]);
    
    NSLog(@"取元素 获取多个元素");
    NSIndexSet *set = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 2)];
    NSArray *array2 = [array1 objectsAtIndexes:set];
    NSLog(@"array2=%@",array2);
    
    NSArray *array3 = [array1 objectsAtIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(2, 2)]];
    NSLog(@"array3=%@",array3);
    
    NSLog(@"取元素 简单方式取多个元素");
    
    NSArray *array4 = [array1 subarrayWithRange:NSMakeRange(1, 2)];
    NSLog(@"array4=%@",array4);
    
    
    NSLog(@"遍历");
    
    for(int loop = 0; loop < [array1 count] ; loop++){
        //[array1 indexOfObject:loop];
        NSLog(@"[%d] =%@ ",loop, array1[loop]);
    }
    
    NSLog(@"快速遍历");
    
    for(NSString *str in array1){
        NSLog(@"快速遍历 =%@ ", str);
    }
    
    NSLog(@"数组的组合-拼接");
    NSString *str1 = [array1 componentsJoinedByString:@"*"];
    NSLog(@"数组的组合-拼接 =%@ ", str1);
    
    NSLog(@"字符串的分割");
    
    NSArray *array5 = [str1 componentsSeparatedByString:@"*"];
    NSLog(@"字符串的分割 =%@ ", array5);
    NSLog(@"字符串的分割 =%@ ", [str1 componentsSeparatedByString:@"-"]);

}

- (void)test9{
    NSLog(@"NSNumber-专门用于封装简单数据类型的类");
    NSLog(@"封装");
    
    int a = 65;
    float b = 20.5;
    NSNumber *aNum = [[NSNumber alloc] initWithInt:a];
    NSNumber *bNum = [[NSNumber alloc] initWithFloat:b];
    
    NSLog(@"aNum = %@  bNum=%@ ",aNum,bNum);
    
    NSLog(@"解封");
    int newA = [aNum intValue];
    float newB = [bNum floatValue];
    NSLog(@"newA = %d  newB = %f",newA,newB);
    NSLog(@"newA intValue = %d",[aNum intValue]);
    NSLog(@"newA floatValue = %f",[aNum floatValue]);
    NSLog(@"newA doubleValue = %f",[aNum doubleValue]);
    NSLog(@"newA charValue = %c",[aNum charValue]);
    
    NSLog(@"bNum floatValue = %f",[bNum floatValue]);
    NSLog(@"bNum floatValue = %.1f",[bNum floatValue]);
    NSLog(@"bNum floatValue = %.2f",[bNum floatValue]);
    NSLog(@"bNum floatValue = %.3f",[bNum floatValue]);
    NSLog(@"bNum floatValue = %.4f",[bNum floatValue]);
}

- (void)test10{
    NSLog(@"NSDictionary不可变字典(k-v)");
    NSLog(@"创建字典");
    NSDictionary *dic1 = [[NSDictionary alloc] init];
    NSDictionary *dic2 = [NSDictionary dictionary];
    NSDictionary *dic3 = @{@"1":@"BeiJing",@"2":@"ShangHai"};
    
    NSLog(@"dic1 = %@",dic1);
    NSLog(@"dic2 = %@",dic2);
    NSLog(@"dic3 = %@",dic3);
    
    NSLog(@"通过对象创建字典");
    NSLog(@"value在前，并且全用逗号分割。nil不能省略");
    NSDictionary *dic4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"BeiJing",@"1",@"ShenZhen" ,@"2",nil];
    NSLog(@"dic4 = %@",dic4);
    
    NSDictionary *dic5 = [[NSDictionary alloc] initWithObjects:@[@"BeiJing",@"ChengDu"] forKeys:@[@"1",@"2"]];
    NSLog(@"dic5 = %@",dic5);
    
   
    NSLog(@"通过已存在的字典创建字典");
    
    NSDictionary *dic6 = [[NSDictionary alloc] initWithDictionary:dic5];
    NSLog(@"dic6 = %@",dic6);
    
    NSLog(@"通过类方法");
    NSDictionary *dic7 = [NSDictionary  dictionaryWithObjectsAndKeys:@"BeiJing",@"1",@"ShenZhen" ,@"2",nil];
    NSDictionary *dic8 = [NSDictionary  dictionaryWithObjects:@[@"BeiJing",@"ChengDu"] forKeys:@[@"1",@"2"]];
    NSDictionary *dic9 = [NSDictionary  dictionaryWithDictionary:dic8];
    
    NSLog(@"dic7 = %@",dic7);
    NSLog(@"dic8 = %@",dic8);
    NSLog(@"dic9 = %@",dic9);
}

- (void)test11{
    NSLog(@"不可变字典(k-v)");
    NSDictionary *dic = @{@"1":@"BeiJing",
                           @"2":@"ShangHai",
                           @"3":@"ShenZhen",
                           @"4":@"ChengDu",
                           @"5":@"GuangZhou"
    };
    
    NSLog(@"dic = %@",dic);
    NSLog(@"dic个数 = %lu",[dic count]);
    NSLog(@"[1] = %@",[dic objectForKey:@"1"]);
    NSLog(@"[2] = %@",[dic objectForKey:@"2"]);
    NSLog(@"[3] = %@",[dic objectForKey:@"3"]);
    NSLog(@"[4] = %@",[dic objectForKey:@"4"]);
    NSLog(@"[1] = %@",dic[@"1"]);
    NSLog(@"[2] = %@",dic[@"2"]);
    NSLog(@"[3] = %@",dic[@"3"]);
    NSLog(@"[4] = %@",dic[@"4"]);
    
    NSLog(@"取出多个元素");
    NSArray *array =[dic objectsForKeys:@[@"1",@"2",@"6"] notFoundMarker:@"NOT FOUND"];
    //NSArray *array1 =[dic objectsForKeys:@[@"1",@"2",@"6"] notFoundMarker:nil];

    NSLog(@"array=%@",array);
    
    NSLog(@"allKeys=%@",[dic allKeys]);
    NSLog(@"allValues=%@",[dic allValues]);
    
    NSLog(@"取出已知value对应的key=%@",[dic allKeysForObject:@"ChengDu"]);
    NSLog(@"遍历");
    
    for(int i = 1;i<[dic count];i++){
        NSString *keyStr = [NSString stringWithFormat:@"%d",i];
        NSLog(@"%d = %@=%@",i,keyStr,dic[keyStr]);
    }
    
    NSLog(@"快速遍历");
    
    for (NSString *key in dic) {
        NSLog(@"%@=%@",key,dic[key]);
    }
}


- (void)test12{
    NSLog(@"可变字典(k-v)");
    NSDictionary *base = @{@"1":@"BeiJing",
                           @"2":@"ShangHai",
                           @"3":@"ShenZhen",
                           @"4":@"ChengDu",
                           @"5":@"GuangZhou"
    };
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:base];
    NSLog(@"dic = %@",dic);
    //增
    [dic setObject:@"WuHan" forKey:@"6"];
    
    //删
    [dic removeObjectForKey:@"1"];
    [dic removeObjectsForKeys:@[@"2",@"3"]];
    
    NSLog(@"dic = %@",dic);
    
    //改
    [dic setValue:@"ChengDu1" forKey:@"4"];
    NSLog(@"改dic = %@",dic);
    
    //替换
    [dic setDictionary:@{@"7":@"NanJin"}];
    
    NSLog(@"全部替换 dic = %@",dic);
    NSLog(@"遍历-同不可变字典相同");
}


//测试单例 
- (void)test13{
    SingleTest *sing1 = [SingleTest shareSingle];
    sing1.money = 1000;
    SingleTest *sing2 = [SingleTest shareSingle];
    sing2.money-=100;
    
    SingleTest *sing3 = [SingleTest shareSingle];
    sing3.money-=100;
    
    
    
    SingleTest *sing4 = [SingleTest shareSingle];
    NSLog(@"money = %d", sing4.money);
}


- (void)testNSDate {
    NSLog(@"Test NSDate");
    TestNSDate *test = [TestNSDate new];
    [test test1];
}



@end
